// pages/detail/detail.js
var app = getApp()
// app.dodetail(2);
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsInfo: {
            'gPic': '20190602062811930.png'
        },
        imgURL: 'http://localhost/store/img/',
        comment: null,
        flag: true,
        mflag: true,
        msg: null,
        sImg: null,
        saveImg: '../../public/icon/icon-save-line.png',
        savedImg: '../../public/icon/icon-saved-line.png',
        sflag: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var $this = this;

        this.setData({
            goodsInfo: app.globalData.goodsInfo
        });
        wx.request({
            url: app.globalData.baseURL + 'Info/getComments',
            method: 'POST',
            data: {
                'gid': app.globalData.goodsInfo.gId
                // 'gid': 17
            },
            header: {

            },
            success: function(res) {
                console.log(res.data);
                $this.setData({
                    comment: res.data
                })
            }
        });
        if (app.globalData.userInfo)
            this.checkSave();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    buy: function (e) {
    // console.log(e);
        wx.navigateTo({
            url: '../order/order',
        })
    },
    sendMessage: function(e) {
        if (app.globalData.userInfo) {
            this.setData({
                mflag: !this.data.mflag
            });
        } else {
            wx.showToast({
                title: '请登录后再试!',
                icon: 'none',
                duration: 1000
            })
        }
    },
    comment: function(e) {
        // console.log(e.currentTarget.id);
        // let gid = e.currentTarget.id;
        // wx.showModal({
        // 	title: '评论信息'
        // })
        if (app.globalData.userInfo) {
            this.setData({
                flag: !this.data.flag
            });
        } else {
            wx.showToast({
                title: '请登录后再试!',
                icon: 'none',
                duration: 1000
            })
        }

    },
    formSubmit: function(e) {
        var $this = this;
        console.log(e.detail.value);
        let gid = e.detail.target.id;
        console.log(gid);
        if (e.detail.value != "") {
            wx.request({
                url: app.globalData.baseURL + 'Action/doComments',
                method: 'POST',
                header: {

                },
                data: {
                    'content': e.detail.value.msg,
                    'gid': gid,
                    'uid': app.globalData.userInfo.userId
                    // 'uid': 1
                },
                success: function(res) {
                    if (res.data == 1) {
                        wx.showToast({
                            title: '评论成功',
                            duration: 1000
                        });
                        $this.onLoad();
                    } else {
                        wx.showToast({
                            title: '评论失败',
                            icon: 'none',
                            duration: 1000
                        });
                    }
                }
            })
        } else {
            wx.showToast({
                title: '请输入内容',
                icon: 'none',
                duration: 1000
            });
        }
        this.setData({
            msg: null
        });
        this.setData({
            flag: true
        });
    },
    mformSubmit: function(e) {
        var $this = this;
        console.log(e.detail.value);
        let uid = e.detail.target.id;
        console.log(uid);
        // if (false) {
        if (app.globalData.userInfo.userId == uid) {
            wx.showToast({
                title: '不能给自己发信息',
                icon: 'none',
                duration: 1000
            });
        } else {
            if (e.detail.value != "") {
                wx.request({
                    url: app.globalData.baseURL + 'Action/doMessage',
                    method: 'POST',
                    header: {

                    },
                    data: {
                        'content': e.detail.value.msg,
                        'fromid': app.globalData.userInfo.userId,
                        // 'fromid': 17,
                        'toid': uid
                        // 'uid': 1
                    },
                    success: function(res) {
                        if (res.data == 1) {
                            wx.showToast({
                                title: '发送成功',
                                duration: 1000
                            });
                            $this.onLoad();
                        } else {
                            wx.showToast({
                                title: '发送失败',
                                icon: 'none',
                                duration: 1000
                            });
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '请输入内容',
                    icon: 'none',
                    duration: 1000
                });
            }
        }
        this.setData({
            msg: null
        });
        this.setData({
            mflag: true
        });
    },
    formReset: function() {

    },
    pressSave: function(e) {
        console.log(e);
        if (app.globalData.userInfo) {
            if (this.data.sflag) {
                this.unSave();
            } else {
                this.doSave();
            }
        } else {
            wx.showToast({
                title: '请先登录',
                icon: 'none',
                duration: 1000
            });
        }
    },
    checkSave: function() {
        var $this = this;
        wx.request({
            url: app.globalData.baseURL + 'User/checkSave',
            method: 'POST',
            data: {
                'gid': app.globalData.goodsInfo.gId,
                'uid': app.globalData.userInfo.userId
            },
            header: {

            },
            success: function(res) {
                if (res.data == 1) {
                    $this.setData({
                        sflag: true
                    });
                } else {
                    $this.setData({
                        sflag: false
                    })
                }
            }
        });
    },
    doSave: function() {
        var $this = this;
        wx.request({
            url: app.globalData.baseURL + 'Action/doSave',
            method: 'POST',
            data: {
                'gid': app.globalData.goodsInfo.gId,
                'uid': app.globalData.userInfo.userId
            },
            header: {

            },
            success: function(res) {
                if (res.data == 1) {
                    $this.setData({
                        sflag: !$this.data.sflag
                    });
                    wx.showToast({
                        title: '收藏成功',
                        duration: 1000
                    });
                }
            }
        });
    },
    unSave: function() {
        var $this = this;
        wx.request({
            url: app.globalData.baseURL + 'Action/cancelSave',
            method: 'POST',
            data: {
                'gid': app.globalData.goodsInfo.gId,
                'uid': app.globalData.userInfo.userId
            },
            header: {

            },
            success: function(res) {
                if (res.data == 1) {
                    $this.setData({
                        sflag: !$this.data.sflag
                    });
                    wx.showToast({
                        title: '收藏成功',
                        duration: 1000
                    });
                }
            }
        });
    }
})